﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ConfigExport.cs" company="">
//   
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace AdminTool
{
    using System;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    using ExecutionTool.ImportExport;
    using ExecutionTool.Profile;

    public partial class ConfigExport : UserControl, IProfileConfigurator
    {
        #region Constructors and Destructors

        public ConfigExport()
        {
            this.InitializeComponent();
            this.checkBoxEnableExport.Checked = false;
        }

        #endregion

        #region Public Methods and Operators

        public void LoadProfile(Profile profile)
        {
            this.checkBoxEnableExport.Checked = profile.Orchestration.Export;
            this.textBoxAccountName.Text = profile.TargetAzureStorage.StorageAccount;
            this.textBoxAccessKey.Text = profile.TargetAzureStorage.AccessKey;
            this.textBoxDescription.Text = profile.TargetAzureStorage.Description;

            this.ResetMessage();
            this.WriterMessage("Profile data loaded.");
        }

        public void SaveProfile(Profile profile)
        {
            profile.Orchestration.Export = this.checkBoxEnableExport.Checked;
            profile.TargetAzureStorage.StorageAccount = this.textBoxAccountName.Text.Trim();
            profile.TargetAzureStorage.AccessKey = this.textBoxAccessKey.Text.Trim();
            profile.TargetAzureStorage.Description = this.textBoxDescription.Text.Trim();

            this.WriterMessage("Profile data saved.");
        }

        #endregion

        #region Methods

        private void ConfigExport_Load(object sender, EventArgs e)
        {
        }

        private void ResetMessage()
        {
            this.textBoxTestConnection.Text = string.Empty;
        }

        private void WriterMessage(string message)
        {
            this.textBoxTestConnection.Text += message;
            this.textBoxTestConnection.Text += Environment.NewLine;
        }

        private void buttonTestConnection_Click(object sender, EventArgs e)
        {
            var accountName = this.textBoxAccountName.Text.Trim();
            var accessKey = this.textBoxAccessKey.Text.Trim();

            var error = false;

            if (string.IsNullOrWhiteSpace(accountName))
            {
                this.WriterMessage("Please specify the account name first.");
                error = true;
            }

            if (string.IsNullOrWhiteSpace(accessKey))
            {
                this.WriterMessage("Please specify the account name first.");
                error = true;
            }

            if (error)
            {
                return;
            }

            this.textBoxTestConnection.Text = string.Empty;
            this.WriterMessage("Starting to test the location...");
            var adapter = new StorageAdapter();
            Task.Factory.StartNew(() => adapter.TestConnection(new AzureStorageLocation { StorageAccount = accountName, AccessKey = accessKey, })).
                ContinueWith(
                    t =>
                        {
                            if (t.IsFaulted && t.Exception != null)
                            {
                                this.WriterMessage("Test failed. -->" + t.Exception);
                            }
                            else
                            {
                                this.WriterMessage("Tested successfully.");
                            }
                        });
        }

        private void checkBoxEnableExport_CheckedChanged(object sender, EventArgs e)
        {
            var cb = (CheckBox)sender;
            this.groupBox1.Enabled = cb.Checked;
            Program.OnExportEnablementChanged(this, new ExportEnablementChangedEventArgs(cb.Checked));
        }

        #endregion
    }
}
